package com.example.malltest.product.controller;


import com.example.malltest.common.R;
import com.example.malltest.product.entity.Product;
import com.example.malltest.product.entity.ProductDTO;
import com.example.malltest.product.entity.ProductDTO1;
import com.example.malltest.product.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("product")
public class ProductController {

    @Autowired
    private ProductService productService;


    /**
     * 商品列表
     * @return
     */
    @GetMapping("list")
    public R<List<Product>> getList(){
        List<Product> products = productService.getList();
        return R.ok(products);
    }

    /**
     * 增加商品
     * @param productDTO
     * @return
     */
    @PostMapping("add")
    public R<Void> add(@RequestBody @Validated ProductDTO productDTO){
        productService.add(productDTO);
        return R.ok();
    }

    /**
     * 删除商品
     * @param id
     * @return
     */
    @DeleteMapping("delete/{id}")
    public R<Void> delete(@PathVariable Integer id){
        productService.delete(id);
        return R.ok();
    }

    /**
     * 编辑商品
     * @param id
     * @param productDTO1
     * @return
     */
    @PutMapping("edit/{id}")
    public R<Void> edit(@PathVariable Integer id, @RequestBody @Validated ProductDTO1 productDTO1){
        productService.edit(id,productDTO1);
        return R.ok();
    }

    /**
     * 更改状态
     * @param id
     * @param status
     * @return
     */
    @PostMapping("status/{id}")
    public R<Void> status(@PathVariable Integer id, @RequestParam String status){
        productService.status(id,status);
        return R.ok();
    }


    /**
     * 上传图片
     * @param id
     * @param image
     * @return
     */
    @PostMapping("image/{id}")
    public R<Void> image(@PathVariable Integer id,@RequestParam("image") MultipartFile image){
        productService.image(id,image);
        return R.ok();
    }

    /**
     * 获取热销商品
     * @return
     */
    @GetMapping("list1")
    public R<List<Product>> getList1(){
        List<Product> products = productService.getList1();
        return R.ok(products);
    }

    /**
     * 待补商品
     * @return
     */
    @GetMapping("list2")
    public R<List<Product>> getList2(){
        List<Product> products = productService.getList2();
        return R.ok(products);
    }

    /**
     * 查询商品
     * @param id
     * @return
     */
    @GetMapping("search/{id}")
    public R<Product> search(@PathVariable Integer id){
        Product product = productService.search(id);
        return R.ok(product);
    }
}
